package controllers

import (
	"github.com/goravel/framework/contracts/http"
	"github.com/goravel/framework/facades"
	"github.com/jinzhu/copier"
	"goravel/app/models"
	"goravel/app/services"
	"goravel/app/types"
)

type SysUserController struct {
	DbServer services.DBServer
}

func NewSysUserController() *SysUserController {
	return &SysUserController{
		DbServer: services.NewDBService(),
	}
}

// Index 管理员列表
// @Summary      管理员列表
// @Tags         SysUser
// @Router       /sys_user [get]
func (c *SysUserController) Index(ctx http.Context) http.Response {
	var sysUsers []models.SysUser
	var sysUsersResp []types.SysUserListResp
	query := facades.Orm().Query().OrderBy("id", "desc")
	data, meta, err := c.DbServer.ListPage(ctx, &sysUsers, query, &sysUsersResp)
	if err != nil {
		return services.NewApiService(ctx).Error500(err.Error())
	}
	return services.NewApiService(ctx).DataPage(data, meta)
}

// Show 管理员详情
// @Summary      管理员详情
// @Tags         SysUser
// @Router       /sys_user [get]
func (c *SysUserController) Show(ctx http.Context) http.Response {
	id := ctx.Request().RouteInt64("id")
	var sysUser models.SysUser
	err := facades.Orm().Query().FindOrFail(&sysUser, id)
	if err != nil {
		return services.NewApiService(ctx).Error500("无相关数据")
	}
	var sysUserResp types.SysUserInfoResp
	err = copier.Copy(&sysUserResp, &sysUser)
	if err != nil {
		return services.NewApiService(ctx).Error500("数据copier错误")
	}
	return services.NewApiService(ctx).Data(&sysUserResp)
}

// Store 新增管理员
// @Summary      新增管理员
// @Tags         SysUser
// @Router       /sys_user [post]
func (c *SysUserController) Store(ctx http.Context) http.Response {
	var sysUserCreateReq types.SysUserCreateReq
	var err error
	err = ctx.Request().Bind(&sysUserCreateReq)
	if err != nil {
		return services.NewApiService(ctx).Error500("参数错误")
	}
	hashedPassword, err := facades.Hash().Make(sysUserCreateReq.Password)
	if err != nil {
		return services.NewApiService(ctx).Error500("秘钥hash错误")
	}
	sysUserCreateReq.Password = hashedPassword
	var sysUser models.SysUser
	err = copier.Copy(&sysUser, &sysUserCreateReq)
	if err != nil {
		return services.NewApiService(ctx).Error500("参数错误")
	}
	err = facades.Orm().Query().Create(&sysUser)
	if err != nil {
		return services.NewApiService(ctx).Error500("数据库插入错误")
	}
	return services.NewApiService(ctx).OK("stored")
}

// Update 更新管理员
// @Summary      更新管理员
// @Tags         SysUser
// @Router       /sys_user/{id} [put]
func (c *SysUserController) Update(ctx http.Context) http.Response {
	id := ctx.Request().RouteInt64("id")
	var sysUserUpdateReq types.SysUserUpdateReq
	var err error
	err = ctx.Request().Bind(&sysUserUpdateReq)
	if err != nil {
		return services.NewApiService(ctx).Error500("参数错误")
	}
	var sysUser models.SysUser
	err = facades.Orm().Query().FindOrFail(&sysUser, id)
	if err != nil {
		return services.NewApiService(ctx).Error500("无相关数据")
	}
	if sysUserUpdateReq.Password != "" {
		hashedPassword, err := facades.Hash().Make(sysUserUpdateReq.Password)
		if err != nil {
			return services.NewApiService(ctx).Error500("秘钥hash错误")
		}
		sysUserUpdateReq.Password = hashedPassword
	}
	err = copier.CopyWithOption(&sysUser, &sysUserUpdateReq, copier.Option{IgnoreEmpty: true})
	if err != nil {
		return services.NewApiService(ctx).Error500("参数copier错误")
	}
	err = facades.Orm().Query().Save(&sysUser)
	if err != nil {
		return services.NewApiService(ctx).Error500("更新错误")
	}
	return services.NewApiService(ctx).OK("updated")
}

// Destroy 删除管理员
// @Summary      删除管理员
// @Tags         SysUser
// @Router       /sys_user/{id} [delete]
func (c *SysUserController) Destroy(ctx http.Context) http.Response {
	id := ctx.Request().RouteInt64("id")
	var sysUser models.SysUser
	err := facades.Orm().Query().FindOrFail(&sysUser, id)
	if err != nil {
		return services.NewApiService(ctx).Error500("无相关数据")
	}
	_, err = facades.Orm().Query().Delete(&sysUser)
	if err != nil {
		return services.NewApiService(ctx).Error500("删除失败")
	}
	//TODO:如果有其他关联表,需要删除关联表数据
	return services.NewApiService(ctx).OK("deleted")
}
